<?php
namespace app\controller;
use app\model\Cat;
use app\model\Goods;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;
class Index{
    public function index(){
        $title = '商城';
        $login = '欧阳克';
//        $map['fid'] = array('neq',0);//查询fid不为0
//        $menu = Db::table('shop_menu')->where('fid',0)->select();
        $left = Db::table('shop_menu')->select();

//        halt($left);

        $all = Request::param();
//        halt($all['status']);
        if(isset($all['status']) && $all['status'] <> -1){
            $where['status'] = $all['status'];
        }else{
            $where = true;
        }
//        halt($where);

        $rightList = Db::table('shop_goods')
              ->where($where)
            ->order('add_time DESC')
            ->order('price')
            ->paginate(['list_rows'=>10,'query'=>Request::param()]);
//            ->paginate(5);
        $right = $rightList;
        View::assign('title',$title);
        View::assign('login',$login);
        View::assign('left',$left);
        View::assign('right',$right);
        View::assign('rightList',$rightList);
//        halt($right);
        View::assign('status',isset($all['status']) ? $all['status']:-1);
        return View::fetch();
    }

    public function edit(){
        if (Request::method() == 'POST'){
            $data = Request::param();
            $result = Db::table('shop_goods')->where('id',$data['id'])->update($data);
            if($result){
                echo json_encode(['status'=>1,'message'=>'success']);
            }else{
                echo json_encode(['status'=>0,'message'=>'fail']);
            }
        }elseif (Request::method() == 'GET'){
            $data = Request::param();
            $id = Request::get('id');
            $shop = Db::table('shop_goods')->where('id',$id)->find();
            $cat = Db::table('shop_cat')->where('status',1)->select();
            View::assign('shop',$shop);
            View::assign('cat',$cat);
            return View::fetch();
        }else{
            echo '操作有误';
        }

    }

    public function add(){
        if (Request::method() == 'POST'){
            $data = Request::param();
            $data['add_time'] = time();
            $result = Db::table('shop_goods')->insert($data);
            if($result){
                echo json_encode(['status'=>1,'message'=>'success']);
            }else{
                echo json_encode(['status'=>0,'message'=>'fail']);
            }
        }else{
            $cat = Db::table('shop_cat')->where('status',1)->select();
            View::assign('cat',$cat);
            return View::fetch();
        }
    }

    public function del(){
        $id = Request::param('id');
        $result = Db::table('shop_goods')->where('id',$id)->delete();
        if($result){
            echo json_encode(['status'=>1,'message'=>'success']);
        }else{
            echo json_encode(['status'=>0,'message'=>'fail']);
        }
    }

    public function shopCat(){
        $title = '分类管理';
        $login = '黄定鑫';

        $Db = new Cat();
//        $right = $Db->getCat();
        $right = Db::table('shop_cat')->select();

        $left = Db::table('shop_menu')->select();

        View::assign('right',$right);
        View::assign('left',$left);
        View::assign('login',$login);
        View::assign('title',$title);
        return View::fetch();
    }

    public function catedit(){
        if(Request::method() == 'POST'){
            $id = Request::param();
            $res = Db::table('shop_cat')->where('id',$id['id'])->update($id);
            if ($res == 1){
                echo json_encode(['status'=>1,'message'=>'suc']);
            }else{
                echo json_encode(['status'=>0,'message'=>'fail']);
            }
        }else{
            $id = Request::param('id');
            $cat = Db::table('shop_cat')->where('id',$id)->find();
            View::assign('cat',$cat);
            return View::fetch('catedit');
        }

    }

    public function catdel(){
        $id = Request::param('id');
        $res = Db::table('shop_cat')->where('id',$id)->delete();
        if ($res){
            echo json_encode(['status'=>1,'message'=>'suc']);
        }else{
            echo json_encode(['status'=>0,'message'=>'fail']);
        }
    }

    public function catadd(){
        if(Request::method() == 'POST'){
            $data = Request::param();
            $res = Db::table('shop_cat')->insert($data);
            if ($res){
                echo json_encode(['status'=>1,'message'=>'suc']);
            }else{
                echo json_encode(['status'=>0,'message'=>'fail']);
            }
        }else{
            return View::fetch();
        }

    }

    public function addAll(){
        $db = new Goods();
        $db->addAll();
    }
}